package br.com.j7.jdesk.ui.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.DualListModel;

import br.com.j7.jdesk.persistence.entity.Pages;
import br.com.j7.jdesk.persistence.entity.UsersGroups;
import br.com.j7.jdesk.persistence.repository.PageRepository;
import br.com.j7.jdesk.persistence.repository.UserGroupRepository;
import br.com.j7.jdesk.persistence.repository.impl.PageRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.UserGroupRepositoryImpl;

/**
 * 
 * @author Edivando Alves
 *
 */
@ManagedBean
@ViewScoped
public class UserGroupBean extends WebContext implements Bean{

	private UsersGroups userGroup = new UsersGroups();
	private List<UsersGroups> userGroups;
	private List<Pages> pages;
	
	private UserGroupRepository repository;
	private PageRepository pageRepository;
	
	private DualListModel<Pages> listPages;
	
	public UserGroupBean() {
		repository = new UserGroupRepositoryImpl();
		pageRepository = new PageRepositoryImpl();
	}
	
	
	@Override
	public String adicionar() {
		try {
//			listPages.getTarget();
			userGroup.setEnterprise(getSessionEnterprise());
			if(userGroup.getIdUserGroup() == null || userGroup.getIdUserGroup().equals(0)){
				userGroup.setIdUserGroup(null);
				repository.add(userGroup);
				addMessage("ui.mobile.user.group.add", userGroup.getCdUserGroup());
			}else {
				repository.update(userGroup);
				addMessage("ui.mobile.user.group.update", userGroup.getCdUserGroup());
			}
			userGroup = new UsersGroups();
			userGroups = null;
			return "userGroup";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public String remover() {
		try {
			if( repository.remove(userGroup) ){
				addMessage("ui.mobile.user.group.remove", userGroup.getCdUserGroup());
				userGroup = null;
				userGroups = null;
			}
			return "userGroup";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<UsersGroups> getUserGroups() {
		try {
			if(this.userGroups == null){
				this.userGroups = repository.listByEnterprise(getSessionEnterprise());
			}
			return userGroups;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<Pages> getPages() {
		try {
			if( pages == null ){
				pages = pageRepository.list();
			}
			return pages;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public DualListModel<Pages> getListPages() {
		if( listPages == null )
			listPages = new DualListModel<>(getPages(), listPageByUserGroup());
		return listPages;
	}
	
	public void setListPages(DualListModel<Pages> listPages) {
		this.listPages = listPages;
	}
	
	public List<Pages> listPageByUserGroup(){
		try {
			List<Pages> list = new ArrayList<>();
			if( getUserGroup() != null ){
				list = pageRepository.listPageByUserGroup(getUserGroup());
			}
			return list;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public UsersGroups getUserGroup() {
		return userGroup;
	}
	
	public void setUserGroup(UsersGroups userGroup) {
		this.userGroup = userGroup;
	}
	
	@Override
	public String toForm(){
		return "userGroupForm";
	}
	
	@Override
	public String toList() {
		userGroup = new UsersGroups();
		return "userGroup";
	}

}
